﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class movie : System.Web.UI.Page
{
    private int pageSize = 40;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitMovieList();
        }
    }
    private void InitMovieList()
    {
        string type = Request["type"];
        string page = Request["page"];
        int p = 1;
        string typeCondition = "";
        //类型不为空且是数字，设置类型条件。否则不加类型条件。
        if (!string.IsNullOrEmpty(type) && int.TryParse(type, out p))
        {
            typeCondition = " where type=" + type;
        }
        else
        {
            type = "";
        }        

        DataRow row = PageData.SelectParamRow("typeCode='movie" + type + "'");
        int total = 1;
        if (row == null)//没有找到，类型不对.设置为默认
        {
            row = PageData.SelectParamRow("typeCode='movie'");
            page = "1";            
        }

        Page.Title = row[PageColoumn.title].ToString();
        HtmlMeta metaDesc = (HtmlMeta)this.Master.FindControl("metaDesc");
        HtmlMeta metaKey = (HtmlMeta)this.Master.FindControl("metaKey");
        metaDesc.Content = row[PageColoumn.description].ToString();
        metaKey.Content = row[PageColoumn.keyword].ToString();

        total = int.Parse(row[PageColoumn.totalCount].ToString());
        int start = PageData.CalculateStart(page, total, pageSize);

        string sql = "select id, title, img, actor, director, year, area from MoviePublish " + typeCondition +
            " order by id desc limit " + start.ToString() + "," + pageSize.ToString();
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            rptMovie.DataSource = reader;
            rptMovie.DataBind();
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }       
    }    
}
